package com.lws.applicationlifecycle;

import android.app.Application;
import android.content.Context;

import java.util.ArrayList;

public class ObserverList<T> extends ArrayList<T> implements AppLifeCycleObserver {


    @Override
    public void attachBaseContext(Context base) {
        //do nothing
    }

    @Override
    public void onCreate() {
        for (int i = 0; i < size(); i++) {
            T t = get(i);
            if (t instanceof AppLifeCycleObserver) {
                ((AppLifeCycleObserver) t).onCreate();
            }
            if (t instanceof Application) {
                ((Application) t).onCreate();
            }
        }
    }

    @Override
    public void onTerminate() {
        for (int i = 0; i < size(); i++) {
            T t = get(i);
            if (t instanceof AppLifeCycleObserver) {
                ((AppLifeCycleObserver) t).onTerminate();
            }
            if (t instanceof Application) {
                ((Application) t).onTerminate();
            }
        }
        clear();
    }
}
